package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.geepi.base.DBUtil;

public class ResourceDetail extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ResourceDetail() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int resourceId = Integer.parseInt(request.getParameter("id"));
		String token = request.getParameter("token");
		String account = request.getParameter("account");
		JSONObject jsonObject = null;		
		int status = -1;
		String message = "";
        Map<String, Object> params = null;

		if (!token.equals("cf7cbe86-270f-4735-82ef-23709a07c249")) {
			status = 100;
			message = "接口验证失败";
		} else {						
			try {
			    String condition = "select * from resource where id =" + resourceId + ";";

				DBUtil dbUtil = new DBUtil();
				dbUtil.open();
				ResultSet rs = dbUtil.query(condition); 

				while (rs.next()) {
					 //声明一个Hash对象并添加数据
					params =  new HashMap<String, Object>();					
					params.put("id", resourceId);
					params.put("title", rs.getString("title"));
					params.put("type", rs.getString("type"));
					params.put("createtime", rs.getString("createtime"));
					params.put("detailInfo", rs.getString("detailInfo"));
					params.put("author", rs.getString("author"));
					params.put("source", rs.getString("source"));
					params.put("thumb", rs.getString("thumb"));
					params.put("detailImages", rs.getString("detailImages"));
				}

				if (account == null || account.equals("")) {
					params.put("iscollect", false);
				} else {
					String querySql = String.format("select count(*) from favorites where account = '%s' and resourceId = '%s';", account, resourceId);
					ResultSet resultSetCount = dbUtil.query(querySql);
					while (resultSetCount.next()) {
						int count = resultSetCount.getInt("count(*)");
						params.put("iscollect", count > 0 ? true : false);
					}
				}
				status = 0;
				message = "接口验证成功";
				

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				status = 105;
				message = e.getMessage();
			} 
		}
		
		Map<String, Object> tempParamsMap = new HashMap<String, Object>();
		tempParamsMap.put("status", status);
		tempParamsMap.put("msg", message);
		tempParamsMap.put("item", params);
		
		jsonObject = JSONObject.fromObject(tempParamsMap);
		
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println(jsonObject);
		out.flush(); // 必须有这行，不然页面不会显示
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
